<?php 
    class Gifts_model extends CI_Model{

        function __construct(){
            parent::__construct();
        }

        function get_list($limit = 0, $offset = 0, $condtions = array()){
            $where = '1=1';
            if(!empty($condtions['name'])){
                $where .= " AND name like '%".$data['name']."%'";
            }
            $sql  = "SELECT SQL_CALC_FOUND_ROWS g.*,c.name as cate_name,u.nick as user_name from  {$this->db->dbprefix}gifts g  
                left join {$this->db->dbprefix}gift_categorys c on c.id = g.category_id
                left join {$this->db->dbprefix}users u on u.id=g.user_id 
                WHERE $where ORDER BY g.id desc ";

            
            if($limit > 0 || $offset > 0){
                $sql .= " LIMIT $limit, $offset";
            }
            $result = $this->db->query($sql)->result_array();
            if($limit > 0 || $offset > 0){
                $row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
                return array("count" => $row_num['row_num'], "result" => $result);
            }else{
                return $result;
            }
        }


        function get_category_list(){
            $sql = "SELECT * FROM {$this->db->dbprefix}gift_categorys";
            $result = $this->db->query($sql)->result_array();
            return $result;
        }




    }